<?php
/**
 * 登录控制器类
 */
namespace Admin\Controller;

use Think\Controller;

class LoginController extends Controller
{
    public function index()
    {
        $str = 'IP地址：'.getClientIP().' 访问了仿layUI社区后台系统';
        $email = '';
        $emailtitle = '有第三方访问了仿layUI社区后台系统';
        $mail = actmail($email, $emailtitle, $str);
        if (!$mail['error']) {
            $title = '欢迎进入仿layui社区后台管理平台';
            $this->assign('title', $title);
            $this->display();
        } else {
            var_dump($mail);
        }
    }
    // 用户登录
    public function login()
    {
        if ($_POST) {
            $get_post = I('post.');
            $condition = array('aname'=>$get_post['username'],'apwd'=>md5($get_post['pass']),'available'=>1);
            $have_admin = M('admin')->where($condition)->find();
            if ($have_admin) {
                $_SESSION['aid'] = $have_admin['aid'];
                $_SESSION['aname'] = $have_admin['aname'];
                $this->log('访问后台系统', null, $type = 5);
                // var_dump($have_admin);exit;
                echo json_encode(array('info'=>'欢迎'.$_SESSION['aname'].'登录后台管理平台'));
            } else {
                echo json_encode(array('error'=>'用户名或密码错误'));
            }
        } else {
            $this->error('非法访问', U('admin/index/index'), 1);
        }
    }
    // 退出登录
    public function logout()
    {
        session_unset();
        session_destroy();
        echo json_encode(['info'=>'退出帐号成功']);
    }
    //记录日志
    public function log($content, $more = null, $type = 1)
    {
        $data['type']=$type;
        $data['content']=$content;
        if ($more != null) {
            $data['more']=serialize($more);
        }
        $data['aname']=$_SESSION['aname'];
        $data['ip']=get_client_ip();
        $data['time']=time();
        M('log')->add($data);
    }
}
